const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    onLoad(){
        this.initNode()
    }
    onEnable(){
        
    }

    onDisable() {
        
    }

    // update (dt) {}

    initNode() {
        let layer = this.node.getComponent(cc.TiledLayer)
        let layerSize = layer.getLayerSize();
        let tiledSize = layer.getMapTileSize();
        for (let y = 0; y < layerSize.height; y++) {
            for (let x = 0; x < layerSize.width; x++) {
                let tiled = layer.getTiledTileAt(x, y, true)
                if (tiled.gid != 0) {
                    tiled.node.group = this.node.group;
                    let body = tiled.node.addComponent(cc.RigidBody)
                    body.type = cc.RigidBodyType.Static
                    let collider = tiled.node.addComponent(cc.PhysicsBoxCollider)
                    collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2);
                    collider.size = tiledSize;
                    collider.apply()
                }
            }
        }
    }
}
